package com.sfd.test.controller;

import com.sfd.core.entity.Stock;
import com.sfd.core.module.Result;
import com.sfd.test.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author haden
 * @date 2022/8/8 18:39
 **/
@RequestMapping("/stock")
@RestController
@Slf4j
public class StockController {
    @Resource
    private StockService stockService;

    @GetMapping("/sub")
    public Result<String> subStock(@RequestParam("id") Long id, @RequestParam("num") Integer num) {
        try {
            stockService.sub(id, num);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.busError(e.getMessage());
        }
        return Result.success();
    }

    @GetMapping("/get")
    public Result<List<Stock>> get() {
        try {
            return Result.success(stockService.list());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.busError(e.getMessage());
        }
    }
}
